Spot-On
spot-on-crypt.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_crypt_h_
29 #define _spoton_crypt_h_
30 
31 #ifdef SPOTON_LINKED_WITH_LIBNTRU
32 extern "C"
33 {
34 #include "../../libNTRU/src/ntru.h"
35 }
36 #endif
37 
38 extern "C"
39 {
40 #include <errno.h>
41 #include <gcrypt.h>
42 #include <openssl/pem.h>
43 #include <openssl/rsa.h>
44 #include <openssl/ssl.h>
45 #include <openssl/x509v3.h>
46 #ifdef SPOTON_LINKED_WITH_LIBPTHREAD
47 #include <pthread.h>
48 #endif
49 }
50 
51 #include <QAtomicInt>
52 #include <QByteArray>
53 #include <QHostAddress>
54 #include <QMutex>
55 #include <QReadWriteLock>
56 #include <QSslCipher>
57 #include <QSslConfiguration>
58 #include <QStringList>
59 
60 #ifdef SPOTON_MCELIECE_ENABLED
61 class spoton_mceliece;
62 #endif
63 class spoton_threefish;
64 
66 {
67  public:
68  static QByteArray derivedSha1Key
69  (const QByteArray &salt,
70  const QString &passphrase,
71  const int hashKeySize,
72  const unsigned long int iterationCount);
73  static QPair<QByteArray, QByteArray> derivedKeys
74  (const QString &cipherType,
75  const QString &hashType,
76  const unsigned long int iterationCount,
77  const QString &passphrase,
78  const QByteArray &salt,
79  const bool singleIteration,
80  QString &error);
81  static QPair<QByteArray, QByteArray> derivedKeys
82  (const QString &cipherType,
83  const QString &hashType,
84  const unsigned long int iterationCount,
85  const QString &passphrase,
86  const QByteArray &salt,
87  const int hashKeySize,
88  const bool singleIteration,
89  QString &error);
90  static QByteArray keyedHash(const QByteArray &data,
91  const QByteArray &key,
92  const QByteArray &hashType,
93  bool *ok);
94  static QByteArray publicKeyEncrypt(const QByteArray &data,
95  const QByteArray &pk,
96  const QByteArray &startsWith,
97  bool *ok);
98  static QByteArray saltedPassphraseHash(const QString &hashType,
99  const QString &passphrase,
100  const QByteArray &salt,
101  QString &error);
102  static QByteArray sha1FileHash(const QString &fileName);
103  static QByteArray sha1FileHash(const QString &fileName,
104  QAtomicInt &atomic);
105  static QByteArray sha1Hash(const QByteArray &data, bool *ok);
106  static QByteArray sha256Hash(const QByteArray &data, bool *ok);
107  static QByteArray sha512Hash(const QByteArray &data, bool *ok);
108  static QByteArray shaXHash(const int algorithm,
109  const QByteArray &data, bool *ok);
110  static QByteArray shake256(const QByteArray &buffer,
111  const size_t length,
112  bool *ok);
113  static QByteArray strongRandomBytes(const size_t size);
114  static QByteArray veryStrongRandomBytes(const size_t size);
115  static QByteArray weakRandomBytes(const size_t size);
116  static QByteArray whirlpoolHash(const QByteArray &data, bool *ok);
117  static QList<QSslCipher> defaultSslCiphers
118  (const QString &sslControlString);
119  static QString publicKeyAlgorithm(const QByteArray &data);
120  static QString publicKeySize(const QByteArray &data);
121  static QString publicKeySizeMcEliece(const QByteArray &data);
122  static QString publicKeySizeNTRU(const QByteArray &data);
123  static QStringList buzzHashTypes(void);
124  static QStringList cipherTypes(void);
125  static QStringList congestionHashAlgorithms(void);
126  static QStringList hashTypes(void);
127  static bool exists(const QByteArray &publicKey, spoton_crypt *crypt);
128  static bool hasShake(void);
129  static bool isValidSignature(const QByteArray &data,
130  const QByteArray &publicKey,
131  const QByteArray &signature);
132  static bool memcmp(const QByteArray &bytes1, const QByteArray &bytes);
133  static bool passphraseSet(void);
134  static const int SHA224_OUTPUT_SIZE_IN_BYTES = 28;
135  static const int SHA384_OUTPUT_SIZE_IN_BYTES = 48;
136  static const int XYZ_DIGEST_OUTPUT_SIZE_IN_BYTES = 64; /*
137  ** The preferred
138  ** digest algorithm's
139  ** output size.
140  */
141  static size_t cipherKeyLength(const QByteArray &cipherType);
142  static size_t ivLength(const QString &cipherType);
143  static void generateMcElieceKeys(const QString &keySize,
144  QByteArray &privateKey,
145  QByteArray &publicKey,
146  bool *ok);
147  static void generateNTRUKeys(const QString &keySize,
148  QByteArray &privateKey,
149  QByteArray &publicKey,
150  bool *ok);
151  static void generateSslKeys(const int rsaKeySize,
152  QByteArray &certificate,
153  QByteArray &privateKey,
154  QByteArray &publicKey,
155  const QHostAddress &address,
156  const long int days,
157  QString &error);
158  static void init(const int secureMemorySize, const bool cbc_cts_enabled);
159  static void memcmp_test(void);
160  static void purgeDatabases(void);
161  static void reencodePrivatePublicKeys
162  (spoton_crypt *newCrypt, spoton_crypt *oldCrypt, const QString &id,
163  QString &error);
164  static void removeFlawedEntries(spoton_crypt *crypt);
165  static void setSslCiphers(const QList<QSslCipher> &ciphers,
166  const QString &sslControlString,
167  QSslConfiguration &configuration);
168  static void terminate(void);
169  spoton_crypt(const QByteArray &privateKey,
170  const QByteArray &publicKey);
171  spoton_crypt(const QString &cipherType,
172  const QString &hashType,
173  const QByteArray &passphrase,
174  const QByteArray &symmetricKey,
175  const int saltLength,
176  const unsigned long int iterationCount,
177  const QString &id);
178  spoton_crypt(const QString &cipherType,
179  const QString &hashType,
180  const QByteArray &passphrase,
181  const QByteArray &symmetricKey,
182  const QByteArray &hashKey,
183  const int saltLength,
184  const unsigned long int iterationCount,
185  const QString &id);
186  spoton_crypt(const QString &cipherType,
187  const QString &hashType,
188  const QByteArray &passphrase,
189  const QByteArray &symmetricKey,
190  const QByteArray &hashKey,
191  const int saltLength,
192  const unsigned long int iterationCount,
193  const QString &id,
194  const QString &modeOfOperation);
195  ~spoton_crypt();
196  QByteArray decrypted(const QByteArray &data, bool *ok);
197  QByteArray decryptedAfterAuthenticated(const QByteArray &data, bool *ok);
198  QByteArray digitalSignature(const QByteArray &data, bool *ok);
199  QByteArray encrypted(const QByteArray &data, bool *ok);
200  QByteArray encryptedThenHashed(const QByteArray &data, bool *ok);
201  QByteArray hashKey(void);
202  QByteArray keyedHash(const QByteArray &data, bool *ok);
203  QByteArray publicKey(bool *ok);
204  QByteArray publicKeyDecrypt(const QByteArray &data, bool *ok);
205  QByteArray publicKeyHash(bool *ok);
206  QByteArray symmetricKey(void);
207  QPair<QByteArray, QByteArray> generatePrivatePublicKeys
208  (const QString &keySize,
209  const QString &keyType,
210  QString &error,
211  const bool save_keys = true);
212  QString cipherType(void) const;
213  QString publicKeyAlgorithm(void);
214  QString publicKeySize(void);
215  QString publicKeySizeMcEliece(void);
216  QString publicKeySizeNTRU(void);
217  bool isAuthenticated(void);
218  qint64 publicKeyCount(void);
219  void purgePrivatePublicKeys(void);
220 
221  private:
222  QAtomicInt m_isMcEliece;
223  QByteArray m_publicKey;
224  QMutex m_cipherMutex;
225  QReadWriteLock m_hashKeyMutex;
226  QReadWriteLock m_privateKeyMutex;
227  QReadWriteLock m_publicKeyMutex;
228  QReadWriteLock m_symmetricKeyMutex;
229  QString m_cipherType;
230  QString m_hashType;
231  QString m_id;
232  char *m_hashKey; // Stored in secure memory.
233  char *m_privateKey; // Stored in secure memory.
234  char *m_symmetricKey; // Stored in secure memory.
235  gcry_cipher_hd_t m_cipherHandle;
236  int m_cipherAlgorithm;
237  int m_hashAlgorithm;
238  int m_saltLength;
239  size_t m_hashKeyLength;
240  size_t m_privateKeyLength;
241  size_t m_symmetricKeyLength;
242 #ifdef SPOTON_MCELIECE_ENABLED
243  spoton_mceliece *m_mceliece;
244 #endif
245  spoton_threefish *m_threefish;
246  static QAtomicInt s_hasSecureMemory;
247  static bool s_cbc_cts_enabled;
248  unsigned long int m_iterationCount;
249  QByteArray publicKeyDecryptMcEliece(const QByteArray &data, bool *ok);
250  QByteArray publicKeyDecryptNTRU(const QByteArray &data, bool *ok);
251  void init(const QString &cipherType,
252  const QString &hashType,
253  const QByteArray &passphrase,
254  const QByteArray &symmetricKey,
255  const QByteArray &hashKey,
256  const int saltLength,
257  const unsigned long int iterationCount,
258  const QString &id,
259  const QString &modeOfOperation);
260  void initializePrivateKeyContainer(bool *ok);
261  void setHashKey(const QByteArray &hashKey);
262  static QByteArray publicKeyEncryptMcEliece(const QByteArray &data,
263  const QByteArray &publicKey,
264  bool *ok);
265  static QByteArray publicKeyEncryptNTRU(const QByteArray &data,
266  const QByteArray &publicKey,
267  bool *ok);
268  static QByteArray randomBytes(const size_t size,
269  const enum gcry_random_level level);
270  static bool setInitializationVector(QByteArray &iv,
271  const int algorithm,
272  gcry_cipher_hd_t cipherHandle);
273  static void generateCertificate(RSA *rsa,
274  QByteArray &certificate,
275  const QHostAddress &address,
276  const long int days,
277  QString &error);
278 };
279 
280 #endif
Definition: spot-on-threefish.h:34
Definition: spot-on-crypt.h:65